/**
 * Copyright (C), 2015-2024, XXX有限公司
 * FileName: App
 * Author: Administrator
 * Date: 2024/8/22 15:23
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
package com.exam.sacrifice;


import android.content.Context;
import android.util.Log;

import androidx.lifecycle.ProcessLifecycleOwner;

import com.exam.commonbiz.base.BasicApplication;
import com.exam.commonbiz.net.NetError;
import com.exam.commonbiz.net.NetProvider;
import com.exam.commonbiz.net.RequestHandler;
import com.exam.commonbiz.net.XApi;
import com.exam.sacrifice.util.AppLifecycleObserver;
import com.pgyersdk.Pgyer;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.smtt.sdk.QbSdk;
import com.tencent.smtt.sdk.TbsListener;

/**16 * @ClassName: App
 * @Description: java类作用描述
 * @Author: Administrator
 * @Date: 2024/8/22 15:23
 */
public class App extends BasicApplication {

    private final String TAG = App.class.getSimpleName();

    public static App mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;

        initHttpConfig();
        // 在Application中注册
        ProcessLifecycleOwner.get().getLifecycle().addObserver(new AppLifecycleObserver());
        initX5();
        initBugly();
    }

    public static App getInstance() {
        if (mInstance == null) {
            mInstance = new App();
        }
        return mInstance;
    }

    private void initBugly(){
        CrashReport.initCrashReport(getApplicationContext(), "94bad9d92d", false);
    }

    @Override
    protected void attachBaseContext(Context context) {
        super.attachBaseContext(context);
        Pgyer.setAppId("71b624896acf3ab422bdc97ff00d3ef3");
    }

    public void initX5(){

        /* 设置允许移动网络下进行内核下载。默认不下载，会导致部分一直用移动网络的用户无法使用x5内核 */
        QbSdk.setDownloadWithoutWifi(true);

        QbSdk.setCoreMinVersion(QbSdk.CORE_VER_ENABLE_202112);
        /* SDK内核初始化周期回调，包括 下载、安装、加载 */

        QbSdk.setTbsListener(new TbsListener() {

            /**
             * @param stateCode 用户可处理错误码请参考{@link com.tencent.smtt.sdk.TbsCommonCode}
             */
            @Override
            public void onDownloadFinish(int stateCode) {
                Log.i(TAG, "onDownloadFinished: " + stateCode);
            }

            /**
             * @param stateCode 用户可处理错误码请参考{@link com.tencent.smtt.sdk.TbsCommonCode}
             */
            @Override
            public void onInstallFinish(int stateCode) {
                Log.i(TAG, "onInstallFinished: " + stateCode);
            }

            /**
             * 首次安装应用，会触发内核下载，此时会有内核下载的进度回调。
             * @param progress 0 - 100
             */
            @Override
            public void onDownloadProgress(int progress) {
                Log.i(TAG, "Core Downloading: " + progress);
            }
        });

        /* 此过程包括X5内核的下载、预初始化，接入方不需要接管处理x5的初始化流程，希望无感接入 */
        QbSdk.initX5Environment(this, new QbSdk.PreInitCallback() {
            @Override
            public void onCoreInitFinished() {
                // 内核初始化完成，可能为系统内核，也可能为系统内核
            }

            /**
             * 预初始化结束
             * 由于X5内核体积较大，需要依赖wifi网络下发，所以当内核不存在的时候，默认会回调false，此时将会使用系统内核代替
             * 内核下发请求发起有24小时间隔，卸载重装、调整系统时间24小时后都可重置
             * 调试阶段建议通过 WebView 访问 debugtbs.qq.com -> 安装线上内核 解决
             * @param isX5 是否使用X5内核
             */
            @Override
            public void onViewInitFinished(boolean isX5) {
                Log.i(TAG, "onViewInitFinished: " + isX5);
                // hint: you can use QbSdk.getX5CoreLoadHelp(context) anytime to get help.
            }
        });
    }

    public void initHttpConfig() {

        String realHost = "http://fete.wqsoftware.cn:8000";
        XApi.registerDefaultProvider(realHost, new NetProvider() {

            @Override
            public okhttp3.Interceptor[] configInterceptors() {
                return new okhttp3.Interceptor[0];
            }

            @Override
            public okhttp3.Interceptor[] configNetInterceptors() {
                return new okhttp3.Interceptor[0];
            }

            @Override
            public okhttp3.CookieJar configCookie() {
                return null;
            }

            @Override
            public RequestHandler configHandler() {
                return null;
            }

            @Override
            public long configConnectTimeoutMills() {
                return 0;
            }

            @Override
            public long configReadTimeoutMills() {
                return 0;
            }

            @Override
            public boolean configLogEnable() {
                return false;
            }

            @Override
            public boolean handleError(NetError error) {
                return false;
            }

            @Override
            public boolean dispatchProgressEnable() {
                return false;
            }

            @Override
            public void configHttps(okhttp3.OkHttpClient.Builder builder) {

            }
        });
    }

}